package server.app.fileupload;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import common.ConfigHelper;

public class FileData
{
    public FileData()
    {
        this.startTime = System.currentTimeMillis();
    }

    private FileDataField fields = new FileDataField();

    long endTime;
    long startTime;
    int step;

    String imageUrl;
    String previewImageUrl;
    int opsTime = 0;
    private String fieldName;

    Map<String, String> attrs;
    Map<FileUploadEnum, String> values;

    /**
     * only response Json content.
     * 
     * @return
     */
    public Map<String, String> getResponseObjWithJSONPrefix()
    {
        Map<String, String> map = new HashMap<String, String>();
        for (Map.Entry<String, String> entry : getAttrs().entrySet())
        {
            String key = entry.getKey();
            if (StringUtils.contains(key, FileUploadEnum.json.toString()))
            {
                String[] names = StringUtils.split(key, "_");
                if (names.length > 2 || names.length <= 1)
                {
                    map.put(key, entry.getValue());
                }
                else
                {
                    map.put(names[1], entry.getValue());
                }

            }
        }
        return map;
    }

    public long getStartTime()
    {
        return startTime;
    }

    public void setStartTime(long startTime)
    {
        this.startTime = startTime;
    }

    public long getEndTime()
    {
        return endTime;
    }

    public void setEndTime(long endTime)
    {
        this.endTime = endTime;
    }

    public int getStep()
    {
        return step;
    }

    public void setStep(int step)
    {
        this.step = step;
    }

    public String getImageUrl()
    {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl)
    {
        this.imageUrl = imageUrl;
    }

    public String getPreviewImageUrl()
    {
        return previewImageUrl;
    }

    public void setPreviewImageUrl(String previewImageUrl)
    {
        this.previewImageUrl = previewImageUrl;
    }

    public int getOpsTime()
    {
        return opsTime;
    }

    public void setOpsTime(int opsTime)
    {
        this.opsTime = opsTime;
    }

    public int increase()
    {
        setOpsTime(getOpsTime() + 1);
        return getOpsTime();
    }

    public String getFieldName()
    {
        return fieldName;
    }

    public void setFieldName(String fieldName)
    {
        this.fieldName = fieldName;
    }

    public Map<String, String> getAttrs()
    {
        return attrs;
    }

    public void setAttrs(Map<String, String> attrs)
    {
        this.attrs = attrs;
    }

    public void addAttr(String key, String value)
    {
        Map<String, String> map = getAttrs();
        if (null == map)
        {
            setAttrs(new HashMap<String, String>());
            map = getAttrs();
        }
        map.put(key, value);
    }

    public void addValue(FileUploadEnum key, String value)
    {
        Map<FileUploadEnum, String> map = getValues();
        if (null == map)
        {
            setValues(new HashMap<FileUploadEnum, String>());
            map = getValues();
        }
        map.put(key, value);
    }

    public FileDataField getFields()
    {
        return fields;
    }

    public void setFields(FileDataField fields)
    {
        this.fields = fields;
    }

    public Map<FileUploadEnum, String> getValues()
    {
        return values;
    }

    public void setValues(Map<FileUploadEnum, String> values)
    {
        if (null == getValues())
        {
            this.values = new HashMap<FileUploadEnum, String>();
        }
        if (null != values)
        {
            this.values.putAll(values);
        }
    }

    public String getName()
    {
        return fields.getName();
    }

    public String getPath()
    {
        return fields.getPath();
    }

    public String getContentType()
    {
        return fields.getContentType();
    }

    public String getMd5()
    {
        return fields.getMd5();
    }

    public long getSize()
    {
        return fields.getSize();
    }

    public void parsed()
    {
        if (null != getAttrs())
        {
            Map<String, String> cleanUpMap = new HashMap<String, String>();
            for (Map.Entry<String, String> entry : getAttrs().entrySet())
            {
                String key = entry.getKey();
                String value = entry.getValue();
                if (StringUtils.contains(value, ConfigHelper.SPLIT_FLAG_AT))
                {
                    for (Map.Entry<FileUploadEnum, String> prepareValue : getValues().entrySet())
                    {
                        String targetStr = prepareValue.getKey().repStr();
                        if (StringUtils.contains(value, targetStr))
                        {
                            value = StringUtils.replace(value, targetStr, prepareValue.getValue());
                        }
                    }
                    // still have the at symbol
                    while (StringUtils.contains(value, ConfigHelper.SPLIT_FLAG_AT))
                    {
                        String[] words =
                                        StringUtils.substringsBetween(value, ConfigHelper.SPLIT_FLAG_START,
                                            ConfigHelper.SPLIT_FLAG_END);
                        System.err.println("Still have can't parsed words : " + StringUtils.join(words, ","));
                        // replace the miss match word to emtpy string.
                        String repStr = String.format("%s%s%s", ConfigHelper.SPLIT_FLAG_START,
                            words[0], ConfigHelper.SPLIT_FLAG_END);
                        if (StringUtils.contains(value, repStr + "/"))
                        {
                            repStr += "/";
                        }
                        value = StringUtils.replace(value, repStr, "");
                    }
                    cleanUpMap.put(key, value);
                }
            }
            getAttrs().clear();
            setAttrs(cleanUpMap);
        }
    }
}
